package com.healthshare.activity.news;


import java.io.FileNotFoundException;

import com.healthshare.R;
import com.healthshare.busi.share.ExposureBusi;
import com.healthshare.parse.RetCodeParse;
import com.palmedia.framework.business.BaseBusi;
import com.palmedia.framework.config.FrameworkConfig;
import com.palmedia.framework.config.LocalData;
import com.palmedia.framework.myinterface.UiCallBack;
import com.palmedia.framework.ui.CustomImageView;
import com.palmedia.framework.ui.CustomMessageShow;
import com.palmedia.framework.zmfile.ZMFile;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class PublishActivity extends Activity  implements UiCallBack
{
	private final static String TAG = "PublishActivity";
	private EditText brandedit;
	private EditText expoedit;
	private EditText contactedit;
	private EditText verifycodedit;
	private ImageView imagepic;
	private String picpath;
	
	private  Bitmap  bitmap;
	
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.publishlayout);
		
		final String domain = FrameworkConfig.getInst().getDomain();
		final com.palmedia.framework.ui.CustomImageView verifycodeimage= (CustomImageView )findViewById(R.id.verifycodeimage);
		verifycodeimage.setImageFromeUrl(domain+"verifycode.php");
		verifycodeimage.isSave=false;
		TextView codereqview=(TextView )findViewById(R.id.codereqview);
		codereqview.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v)
			{
				verifycodeimage.setImageFromeUrl(domain+"verifycode.php");
				
			}});
		
		((Button)this.findViewById(R.id.backbutton)).setVisibility(View.VISIBLE);
		((Button)this.findViewById(R.id.backbutton)).setTextColor(Color.rgb(0,139,0));
		
		((Button)this.findViewById(R.id.rightbutton)).setVisibility(View.VISIBLE);
		((Button)this.findViewById(R.id.rightbutton)).setTextColor(Color.rgb(0,139,0));
		((Button)this.findViewById(R.id.rightbutton)).setText("提交");
		
		brandedit=(EditText )findViewById(R.id.brandedit);
		expoedit=(EditText )findViewById(R.id.expoedit);
		contactedit=(EditText )findViewById(R.id.contactedit);
		verifycodedit=(EditText )findViewById(R.id.verifycodedit);
		
		imagepic=(ImageView )findViewById(R.id.imagepic);
		
		imagepic.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v)
			{
				showChooser();
			}});
		
	}
	
	
	private void showChooser()
	{
		CharSequence[] items = {"相册", "相机"};  
	    new AlertDialog.Builder(this)
		    .setTitle("选择图片来源").setItems(items, new DialogInterface.OnClickListener() 
		    {
				public void onClick(DialogInterface dialog, int which)
				{
					if( which == 0 )
					{
						Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
						intent.addCategory(Intent.CATEGORY_OPENABLE);
						intent.setType("image/*");
						startActivityForResult(Intent.createChooser(intent, "选择图片"), 0); 
			        }
					else if( which == 1 )
					{
			        	Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
			        	startActivityForResult(intent, 1);  
			        }
				}
			})
			.create().show();
	}
	public void backButtonAction(View view)
	{
		this.finish();
	}
	
	public void rightButtonAction(View view)
	{
		reqExposureBusi();
	}
	
	private void reqExposureBusi()
	{
		ExposureBusi eb = new ExposureBusi(this);
		eb.brand = brandedit.getText().toString();
		eb.contact = contactedit.getText().toString();
		eb.content = expoedit.getText().toString();
		eb.verifycode = verifycodedit.getText().toString();
		eb.userid=LocalData.getInst().getLocalValue("userid");
		eb.filePath = picpath;
		if(eb.verifycode==null || eb.verifycode.length()<4)
		{
			CustomMessageShow.getInst().showShortToast(this, "验证码必须填写4个字符");
			return;
		}
		if(eb.content==null || eb.content.length()<10)
		{
			CustomMessageShow.getInst().showShortToast(this, "内容填写必须超过10个字符");
			return;
		}
		eb.iExecute();
	}


	/* (non-Javadoc)
	 * @see com.palmedia.framework.myinterface.UiCallBack#uiCallBack(com.palmedia.framework.business.BaseBusi)
	 */
	@Override
	public void uiCallBack(BaseBusi arg0)
	{
		// TODO Auto-generated method stub
		if(arg0 instanceof ExposureBusi)
		{
			RetCodeParse rp =  (RetCodeParse)arg0.getBaseStruct();
			if(rp.statuscode.equals("0"))
			{
				CustomMessageShow.getInst().showShortToast(this, rp.status);
				this.finish();
			}
			else
			{
				CustomMessageShow.getInst().showShortToast(this, rp.status);
			}
		}
		//reqExposureBusi();
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_OK)
		{
			//选择图片
			Uri uri = data.getData(); 
			CustomMessageShow.getInst().showConfirmDialog(this, uri.toString(),null);
			String [] proj={MediaStore.Images.Media.DATA};
			Cursor cursor = managedQuery( uri,
					proj,                 // Which columns to return
					null,       // WHERE clause; which rows to return (all rows)
					null,       // WHERE clause selection arguments (none)
					"_id  limit 2");                 // Order-by clause (ascending by name)
			
			int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
			cursor.moveToFirst();
			
			String path = cursor.getString(column_index);
			int filesize = ZMFile.getSize(path);
			if(filesize>0)
			{
					
				//ContentResolver cr = this.getContentResolver(); 
				int maxsize=100*1024;//设置的一个阀值，当超过这个值的时候就就用BitmapFactory.Options，压缩图片质量以防止内存溢出
				BitmapFactory.Options options=null;
				if(filesize>=maxsize)
				{
					 options= new BitmapFactory.Options();
					 options.inSampleSize = (int)(filesize/maxsize);
					// options.outHeight=20;
					// options.outWidth=60;
				}
				 
				Bitmap tmpbitmap = BitmapFactory.decodeFile(path, options);//.decodeStream(cr.openInputStream(uri),options);
				
				if(bitmap != null)//如果不释放的话，不断取图片，将会内存不够
					bitmap.recycle();
				
				bitmap=tmpbitmap;
				
				picpath=path;
				imagepic.setImageBitmap(bitmap);
			}
			else
			{
				CustomMessageShow.getInst().showShortToast(this, "请重新选择图片");
			}
		}
		else
		{
			CustomMessageShow.getInst().showShortToast(this, "请重新选择图片");
		}
			
	}
				
		
}
